//
//  PrivilegeViewController.m
//  Molian
//
//  Created by 胡津 on 15/4/1.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//

#import "PrivilegeViewController.h"
#import "HJActionSheet.h"
#import "WEBViewController.h"

@interface PrivilegeViewController ()<UITableViewDelegate,UITableViewDataSource,HJActionSheetDelegate>
//视图列表
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSArray *titleArr;


@end

@implementation PrivilegeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = APPCOLORBACKGROUNDCOLOR(1);
    self.title = @"我的特权";
    self.titleArr = @[@"设置通话资费",@"如何成为一名成功的虚拟恋人"];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth, DeviceHeight)style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollsToTop = YES;
    self.tableView.separatorColor = RGBA(225, 225, 225, 1);//分隔条颜色
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 20, 0);//偏移量
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 20, 0);//滚动条偏移量
    self.tableView.tableHeaderView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"top_tequan_image"]];
    self.tableView.tableFooterView = [[UIView alloc] init];//不显示空的cell
    [self.view addSubview:self.tableView];
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static  NSString *CellIdentifier = @"PrivilegeCell" ;
    //自定义cell类
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if  (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    //防止产生重影,首先移除子视图
    NSArray *subviews = [[NSArray alloc]initWithArray:cell.contentView.subviews];
    for (UIView *subview in subviews) {
        [subview removeFromSuperview];
    }
     [cell setSeparatorInset:UIEdgeInsetsZero];
    //cell的右边有一个小箭头，距离右边有十几像素；
    UILabel *cellTitle = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, DeviceWidth-50, 50)];
    cell.backgroundColor = [UIColor whiteColor];
    cellTitle.textColor = RGBA(51, 51, 51, 1);
    cellTitle.font = [UIFont fontWithName:FontName size:16];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
   
    cellTitle.text = self.titleArr[indexPath.section];
    [cell.contentView addSubview:cellTitle];
    
    if (indexPath.section == 0) {
        UILabel *cellmoney = [[UILabel alloc]initWithFrame:CGRectMake(DeviceWidth-210, 0, 180, 50)];
        cellmoney.textColor = [UIColor grayColor];
        cellmoney.font = [UIFont fontWithName:FontName size:16];
        MLUser *u = [MLUser currentUser];
        [u refreshInBackgroundWithBlock:^(AVObject *object, NSError *error) {
            cellmoney.text = [NSString stringWithFormat:@"%.0f元/分钟" ,((u.price == 0)? 0.5:u.price)];
        }];
        cellmoney.tag = 100;
        cellmoney.textAlignment = NSTextAlignmentRight;
        [cell.contentView addSubview:cellmoney];
    }
    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        HJActionSheet *seet = [[HJActionSheet alloc]initWithTitle:@"设置通话资费" Delegate:self destructiveButtonTitle:@"取消" OtherButtonTitles:@[@"0.5元/分钟",@"1.0元/分钟"]];
        [seet show];
    }else if (indexPath.section == 1){
        WEBViewController *webview = [[WEBViewController alloc]init];
        webview.WEBURL = @"http://www.pocketsweet.com/howtobe";
        [self.navigationController pushViewController:webview animated:YES];
    }
    
}

- (void)hjActionSheet:(UIButton *)sender clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex < 2) {
        [AVAnalytics event:@"特权资费设置"]; // 通知服务器一个点击事件。
        UILabel *label = (UILabel *)[self.view viewWithTag:100];
        MLUser *u = [MLUser currentUser];
        if ([sender.titleLabel.text isEqualToString:@"1.0元/分钟"]) {
            u.price = 1;
        }else if([sender.titleLabel.text isEqualToString:@"0.5元/分钟"]){
            u.price = 0.5;
        }
                [u saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                label.text = sender.titleLabel.text;
            }
        }];
    }
    
}

//section个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;//Cell个数
}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 50;//每个cell的头部间距
//}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5;//每个cell的头部间距
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
{
    return 5;//每个cell的底部间距
}

//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if ([self isViewLoaded]&&!self.view.window) {
        self.view = nil;
    }
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
